package com.iot.platform.mvc.product.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.iot.common.core.exception.ModelException;
import com.iot.common.core.util.R;
import com.iot.common.core.util.UuidUtil;
import com.iot.common.security.annotation.SecurityAuthorization;
import com.iot.platform.api.domain.ProductCategory;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;

import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

@RestController
@RequiredArgsConstructor
@RequestMapping("/product/category")
@Api(value = " ProductCategory", tags = "产品品类")
public class ProductCategoryController {

	@Setter
	private Throwable cause;

	@SecurityAuthorization
	@GetMapping(value = { "/list" }, produces = "application/json; charset=utf-8")
	public R<Page<ProductCategory>> list(@RequestParam(name = "keywords", required = false, defaultValue = "") String keywords,
			@RequestParam(name = "productId", required = false, defaultValue = "") String productId,
			@RequestParam(name = "scene", required = false, defaultValue = "") String scene,
			@RequestParam(name = "area", required = false, defaultValue = "") String area,
			@RequestParam(name = "cur", required = false, defaultValue = "1") int cur,
			@RequestParam(name = "limit", required = false, defaultValue = "10") int limit) throws Exception {
		String sqlExceptSelect = " FROM " + ProductCategory.TABLE_NAME + " O  WHERE 1=1 ";
		if (!StrKit.isBlank(keywords)) {
			sqlExceptSelect += " AND ( O.NAME LIKE '%" + keywords + "%' OR   O.CODE LIKE '%" + keywords + "%' ) ";
		}
		if (!StrKit.isBlank(area)) {
			sqlExceptSelect += " AND  O.AREA = '" + area + "'  ";
		}
		if (!StrKit.isBlank(scene)) {
			sqlExceptSelect += " AND  O.SCENE = '" + scene + "'  ";
		}
		sqlExceptSelect+=" ORDER BY O.SORT ASC" ;
		Page<ProductCategory> pages = ProductCategory.dao.paginate(cur, limit, "select O.* ", sqlExceptSelect);
		return R.ok(pages);
	}
	
	

	@SecurityAuthorization
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<ProductCategory> save(@Valid ProductCategory category) {
		if (category == null) {
			throw new ModelException("对象不可以为空！");
		}
		if (StrKit.isBlank(category.getId())) {
			category.setId(UuidUtil.getUUID());
			if (category.save()) {
				return R.ok(category);
			}
		} else {
			if (category.update()) {
				return R.ok(category);
			}
		}
		return R.failed();
	}

	@SecurityAuthorization
	@GetMapping(value = { "/deleteById" }, produces = "application/json; charset=utf-8")
	public R<Boolean> deleteById(@RequestParam(name = "id", required = true) String id) throws Exception {
		if (ProductCategory.dao.deleteById(id)) {
			return R.ok(true);
		} else {
			return R.failed(false);
		}
	}

	@SecurityAuthorization
	@GetMapping(value = { "/selectTree" }, produces = "application/json; charset=utf-8")
	public R<List<ProductCategory>> selectTree(
			@RequestParam(name = "keywords", required = false, defaultValue = "") String keywords,
			@RequestParam(name = "rootId", required = false, defaultValue = "-1") String rootId) throws Exception {
		List<ProductCategory> ProductCategorys = ProductCategory.dao
				.find("SELECT T.*,(CASE  WHEN (SELECT COUNT(ID) AS COUNT FROM " + ProductCategory.TABLE_NAME
						+ " WHERE PARENT_ID=T.ID)>0 THEN 1 ELSE  0 END )  as hasChildren FROM "
						+ ProductCategory.TABLE_NAME + " T WHERE T.PARENT_ID=? ORDER BY T.SORT ASC ", rootId);
		return R.ok(ProductCategorys);
	}

	@SecurityAuthorization
	@GetMapping(value = { "/listData" }, produces = "application/json; charset=utf-8")
	public R<Page<ProductCategory>> listData(
			@RequestParam(name = "keywords", required = false, defaultValue = "") String keywords,
			@RequestParam(name = "area", required = false, defaultValue = "") String area,
			@RequestParam(name = "scene", required = false, defaultValue = "") String scene,
			@RequestParam(name = "cur", required = false, defaultValue = "1") int cur,
			@RequestParam(name = "limit", required = false, defaultValue = "10") int limit) throws Exception {

		String sqlExceptSelect = " FROM " + ProductCategory.TABLE_NAME + " o  WHERE 1=1 ";
		if (!StrKit.isBlank(area)) {
			sqlExceptSelect += " AND o.AREA='" + area + "' ";
		}
		if (!StrKit.isBlank(scene)) {
			sqlExceptSelect += " AND o.SCENE='" + scene + "' ";
		}
		if (!StrKit.isBlank(keywords)) {
			sqlExceptSelect += " AND O.NAME LIKE '%" + keywords + "%' ";
		}
		sqlExceptSelect += "  ORDER BY   O.sort asc ";
		return R.ok(ProductCategory.dao.paginate(cur, limit, "select * ", sqlExceptSelect));
	}

}
